/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: xmessage_impl.cpp
 *
 * Purpose: message base implementation
 *
 * Developer:
 *   wen.gu , 2023-10-26
 *
 * TODO:
 *
 ***************************************************************************/

#include "xbus/com/xmessage_impl.h"

namespace xbus::com {



XMessageImpl::~XMessageImpl() {
    if (msg_data_) {
        delete[] msg_data_;
        msg_data_ = nullptr;
    }
}

XMessageImpl::XMessageImpl(uint8_t* buf)
    :msg_data_(buf) {
    /** todo something */
}

XMessageImpl::XMessageList XMessageImpl::unpack(uint32_t pkt_size) {
    uint32_t base_hdr_size = XMessage::GetHeaderLength(0);
    uint32_t tp_hdr_size = XMessage::GetHeaderLength(XMSG_FLAGS_TP_FLAG_MASK);
    uint32_t max_tp_msg_size = pkt_size + tp_hdr_size;
    uint32_t msg_payload_size = payload_size();
    uint32_t total_pkg = (msg_payload_size % max_tp_msg_size) ? ((msg_payload_size / max_tp_msg_size) + 1) : (msg_payload_size / max_tp_msg_size);
    uint8_t* pdata = payload();
    uint8_t* pend = pdata + msg_payload_size;
    uint32_t pkg_idx = XMSG_PKT_IDX_START;
    XMessageList msg_list;
    
    while (pdata < pend) {
        uint32_t left_size = static_cast<uint32_t>(pend - pdata);
        uint32_t payload_size = (left_size > max_tp_msg_size) ? max_tp_msg_size : left_size;
        uint32_t length = payload_size + tp_hdr_size;
        uint8_t* buf = new uint8_t[length];
        memcpy(buf, pdata, base_hdr_size); /** initialize base header info */
        internal::ByteWrite4(buf, length);

        XMessageSp msg_ptr{new XMessageImpl(buf)};
        XMSG_FLAGS_TP_FLAG_ENABLE(buf[XMSG_FIELD_OFFSET_FLAGS]);
        
        msg_ptr->set_pkt_total(total_pkg);
        msg_ptr->set_pkt_idx(pkg_idx++);
        msg_list.push_back(msg_ptr);

        pdata += payload_size;       
    }

    return msg_list;
}

//static 
XMessageSp XMessageImpl::CreateMsg(uint32_t payload_size, const uint8_t* payload /*= nullptr*/) {
    uint32_t length = XMessage::GetHeaderLength(0) + payload_size;
    uint8_t* buf = new uint8_t[length];

    if (buf) {
        XMessageSp msg_ptr{new XMessageImpl(buf)};
        XMessage::InitializeMessageHeader(buf, length, 0);
        return msg_ptr;
    }

    return nullptr;
}

//static 
XMessageSp XMessageImpl::CreateTpMsg(uint32_t payload_size, const uint8_t* payload /*= nullptr*/ ) {
    uint32_t length = XMessage::GetHeaderLength(XMSG_FLAGS_TP_FLAG_MASK) + payload_size;
    uint8_t* buf = new uint8_t[length];

    if (buf) {
        XMessageSp msg_ptr{new XMessageImpl(buf)};
        XMessage::InitializeMessageHeader(buf, length, XMSG_FLAGS_TP_FLAG_MASK);
        return msg_ptr;
    }

    return nullptr;
}

//static 
XMessageSp XMessageImpl::BuildMsg(uint8_t* msg_data, uint32_t msg_length) {
    if (!msg_data || msg_length < XMessage::GetHeaderLength(0)) {
        return nullptr;
    }

    if (!XMessage::CheckMessageHeader(msg_data, msg_length)) {
        return  nullptr;
    }

    XMessageSp msg_ptr{new XMessageImpl(msg_data)};

    return msg_ptr;
}

XMessageSp XMessageImpl::CopyMsg(const uint8_t* msg_data, uint32_t msg_length) {
    if (!msg_data || msg_length < XMessage::GetHeaderLength(0)) {
        return nullptr;
    }

    if (!XMessage::CheckMessageHeader(msg_data, msg_length)) {
        return  nullptr;
    }

    uint8_t* buf = new uint8_t[msg_length];
    if (buf) {
        memcpy(buf, msg_data, msg_length);
        XMessageSp msg_ptr{new XMessageImpl(buf)}; 
        return msg_ptr;
    }

    return nullptr;
}

} //namespace xbus::com